﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ADNLib;

namespace ADNActions
{
	public static class Align
	{
        public static void AlignLeft(List<XObject> selection)
        {
            //find leftmost point
            PointF pt = new PointF(0,0);

            if (selection.Count > 0) { pt.X = selection[0].Bounds.Left; }
            else { return; }

            for (int i = 1; i < selection.Count; i++)
            {
                if (selection[i].Bounds.Left < pt.X)
                {
                    pt.X = selection[i].Bounds.Left;
                }
            }

            for (int i = 0; i < selection.Count; i++)
            {
                PointF amount = new PointF(pt.X - selection[i].Bounds.Left, 0);
                selection[i].Move(amount);
            }
            
        }

        public static void AlignRight(List<XObject> selection)
        {
            //find right most point
            PointF pt = new PointF(0, 0);

            if (selection.Count > 0) { pt.X = selection[0].Bounds.Right; }
            else { return; }

            for (int i = 1; i < selection.Count; i++)
            {
                if (selection[i].Bounds.Right > pt.X)
                {
                    pt.X = selection[i].Bounds.Right;
                }
            }

            for (int i = 0; i < selection.Count; i++)
            {
                PointF amount = new PointF(pt.X - selection[i].Bounds.Right, 0);
                selection[i].Move(amount);
            }
        }

        public static void AlignCenter(List<XObject> selection)
        {
            //find center point
            float avgX = 0f;

            if (selection.Count == 0) { return; }

            for (int i = 0; i < selection.Count; i++)
            {
                avgX += selection[i].CenterPoint.X;
            }

            avgX /= selection.Count;

            for (int i = 0; i < selection.Count; i++)
            {
                PointF amount = new PointF(avgX - selection[i].CenterPoint.X, 0);
                selection[i].Move(amount);
            }
        }

        public static void AlignTop(List<XObject> selection)
        {
            //find top most point
            PointF pt = new PointF(0, 0);

            if (selection.Count > 0) { pt.Y = selection[0].Bounds.Top; }
            else { return; }

            for (int i = 1; i < selection.Count; i++)
            {
                if (selection[i].Bounds.Top < pt.Y)
                {
                    pt.Y = selection[i].Bounds.Top;
                }
            }

            for (int i = 0; i < selection.Count; i++)
            {
                PointF amount = new PointF(0, pt.Y - selection[i].Bounds.Top);
                selection[i].Move(amount);
            }
        }

        public static void AlignBottom(List<XObject> selection)
        {
            //find bottom most point
            PointF pt = new PointF(0, 0);

            if (selection.Count > 0) { pt.Y = selection[0].Bounds.Bottom; }
            else { return; }

            for (int i = 1; i < selection.Count; i++)
            {
                if (selection[i].Bounds.Bottom > pt.Y)
                {
                    pt.Y = selection[i].Bounds.Bottom;
                }
            }

            for (int i = 0; i < selection.Count; i++)
            {
                PointF amount = new PointF(0, pt.Y - selection[i].Bounds.Bottom);
                selection[i].Move(amount);
            }
        }

        public static void AlignMiddle(List<XObject> selection)
        {
            //find middle point
            float avgY = 0f;

            if (selection.Count == 0) { return; }

            for (int i = 0; i < selection.Count; i++)
            {
                avgY += selection[i].CenterPoint.Y;
            }

            avgY /= selection.Count;

            for (int i = 0; i < selection.Count; i++)
            {
                PointF amount = new PointF(0, avgY - selection[i].CenterPoint.Y);
                selection[i].Move(amount);
            }
        }

        public static void DistributeHoriz(List<XObject> selection)
        {
            //TODO: sort by left to right

            //find average horizontal gap
            float avgGap = 0;
            if (selection.Count < 2) { return; }
            for (int i = 0; i < selection.Count - 1; i++)
            {
                avgGap += selection[i + 1].Bounds.Left - selection[i].Bounds.Right;
            }

            avgGap /= selection.Count - 1;

            for (int i = 0; i < selection.Count - 1; i++)
            {
                float gap = selection[i + 1].Bounds.Left - selection[i].Bounds.Right;
                selection[i + 1].Move(new PointF(avgGap - gap, 0));
            }
        }

        public static void DistributeVert(List<XObject> selection)
        {
            //find average vertical gap
			float avgGap = 0;
			if (selection.Count < 2) { return; }
			for (int i = 0; i < selection.Count - 1; i++)
			{
				avgGap += selection[i + 1].Bounds.Top - selection[i].Bounds.Bottom;
			}

			avgGap /= selection.Count - 1;

			for (int i = 0; i < selection.Count - 1; i++)
			{
				float gap = selection[i + 1].Bounds.Top - selection[i].Bounds.Bottom;
				selection[i + 1].Move(new PointF(avgGap - gap, 0));
			}
        }
	}
}
